package com.gdqy.FCS.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.Collection;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Fabric;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.CollectionService;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.FabricService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * @author 张伟键
 *类描述：此类开发完毕。等待页面对接
 */
@Controller
@RequestMapping("collection")
public class CollectionController {
	@Autowired
	private CollectionService collectionService;
	@Autowired
	private DesignService designService;
	@Autowired
	private ClothingService clothingService;
	@Autowired
	private FabricService fabricService;
	
	
	// 处理收藏作品页面跳转
	@RequestMapping("deal_collection")
	public ModelAndView deal_collection(@RequestParam(value = "user_id", required = true) int user_id,
			String object_type, Args args, int pageindex, int pagesize) {
		ModelAndView modelAndView = new ModelAndView();
	
		PageHelper.startPage(pageindex, pagesize);
	if(object_type.equals("1")){
		//作品
		List<Collection> collections = collectionService.selectWithDesign(user_id);
		PageInfo<Collection> page = new PageInfo<Collection>(collections);

		Object data = FastResponse._().errCode(0).subCode(0).errMsg("获取收藏作品成功").args(args.getReqeustargs())
				.pageIndex(page.getPageNum()).pageSize(page.getSize()).total((int) page.getTotal())
				.pageCount(page.getPages()).hasNext(page.isHasNextPage()).list(collections);

		modelAndView.addObject("data", data);
		modelAndView.setViewName("common/collection_design");
		return modelAndView;
	}else if(object_type.equals("2")){
		//服装
		List<Collection> collections = collectionService.selectWithClothing(user_id);
		PageInfo<Collection> page = new PageInfo<Collection>(collections);

		Object data = FastResponse._().errCode(0).subCode(0).errMsg("获取收藏作品成功").args(args.getReqeustargs())
				.pageIndex(page.getPageNum()).pageSize(page.getSize()).total((int) page.getTotal())
				.pageCount(page.getPages()).hasNext(page.isHasNextPage()).list(collections);

		modelAndView.addObject("data", data);
		modelAndView.setViewName("common/collection_clothing");
		return modelAndView;
	}else if(object_type.equals("3")){
		//面料
		List<Collection> collections = collectionService.selectWithFabric(user_id);
		PageInfo<Collection> page = new PageInfo<Collection>(collections);

		Object data = FastResponse._().errCode(0).subCode(0).errMsg("获取收藏作品成功").args(args.getReqeustargs())
				.pageIndex(page.getPageNum()).pageSize(page.getSize()).total((int) page.getTotal())
				.pageCount(page.getPages()).hasNext(page.isHasNextPage()).list(collections);

		modelAndView.addObject("data", data);
		modelAndView.setViewName("common/collection_fabric");
		return modelAndView;
	}
			
		modelAndView.setViewName("common/error");	
		return modelAndView;	

	}

	// 收藏作品

	/**
	 * 
	 * 作者：张伟键 方法描述：
	 * 
	 * @param user_id
	 *            用户id
	 * @param object_id
	 *            收藏的id
	 * @param object_type
	 *            收藏的类型
	 * @return
	 */
	@RequestMapping("collect_obj")
	public @ResponseBody String collect_obj(@RequestParam(value = "user_id", required = true) int user_id,
			@RequestParam(value = "object_id", required = true) int object_id,
			@RequestParam(value = "object_type", required = true) String object_type) {

		String msg = "fail";
		Collection collection = collectionService.selectByUser_idAndObject_idAndObject_type(user_id, object_id,
				object_type);
		if (collection != null) {
			msg = "collected";
			return msg;
		} else {
			Collection in_collection = new Collection();
			in_collection.setUser_id(user_id);
			in_collection.setObject_type(object_type);
			in_collection.setObject_id(object_id);
			in_collection.setDatetime(new Date());

			int flag = collectionService.insert(in_collection);

			if (flag > 0) {
				//判断收藏类型，响应加1
				//判断类型
				switch (object_type) {
				case "1":
					//作品
					Design design = designService.selectById(object_id);
					design.setCollected(design.getCollected()+1);
					designService.update(design);
					msg = "success";
					break;
				case "2":
					//服装
					Clothing clothing = clothingService.selectById(object_id);
					clothing.setCollected(clothing.getCollected()+1);
					clothingService.update(clothing);
					msg = "success";
					break;
				case "3":
					//面料
					Fabric fabric = fabricService.selectById(object_id);
					fabric.setCollected(fabric.getCollected()+1);
					fabricService.update(fabric);
					msg = "success";
					break;
					default:
						break;
				}
				
			} else {
				msg = "fail";
			}
		}
		return msg;

	}

	// 删除收藏作品
	@RequestMapping("delete_collection")
	public @ResponseBody String delete_collection(int collection_id, HttpSession session) {
		String msg = "fail";
		// 获取session中的user_id
		int session_uid = (int) session.getAttribute("user_id");
		// 判断收藏是否属于该用户
		Collection collection = collectionService.selectById(collection_id);
		if (session_uid == collection.getUser_id()) {
			int data = collectionService.deleteById(collection_id);
			if (data > 0) {
				msg = "success";
			}
		}
		return msg;
	}

}
